package com.fdb.efp.nls.service.impl.yed.timeTask;

import com.fdb.efp.nls.service.facade.queue.NlsProcessBizService;
import com.fdb.efp.nls.service.facade.yed.YedRepayPlanService;
import com.fdb.efp.nls.service.impl.yed.repay.YedRepaymentInfoSyncServiceImpl;
import com.fdb.efp.nls.service.vo.queue.NlsProcessBizVO;
import com.fdb.efp.nls.service.facade.AccLoanService;
import com.fdb.efp.nls.service.facade.LoanRepayDetailService;
import com.fdb.efp.loan.service.vo.LoanRepayDetailVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * TODO: <br>
 * <p>
 * 0.1:caoxin:2020/9/14 15:53: TODO: <br>
 *
 * @author caoxin
 * @version 0.1
 * @date 2020/9/14 15:53
 * @since 2020/9/14 15:53
 */
@Service("yedRepayPlanService")
public class YedRepayPlanServiceImpl implements YedRepayPlanService {
    private static Logger logger = LoggerFactory.getLogger(YedRepayPlanServiceImpl.class);

    @Autowired
    @Qualifier("yedRepaymentInfoSyncService")
    private YedRepaymentInfoSyncServiceImpl yedRepaymentInfoSyncService;

    @Autowired
    @Qualifier("loanRepayDetailService")
    private LoanRepayDetailService loanRepayDetailService;

    @Autowired
    @Qualifier("nlsProcessBizService")
    private NlsProcessBizService nlsProcessBizService;

    @Autowired
    @Qualifier("accLoanService")
    private AccLoanService accLoanService;

    @Override
    public void startRepaySoltService(){
        logger.info(">>>>>>>>>优e贷还款明细补偿任务开始<<<<<<<<<<<");
        List<LoanRepayDetailVO> loanRepayDetailVOS =  loanRepayDetailService.queryByWfStsAndRsSts();
        if (Objects.isNull(loanRepayDetailVOS) || loanRepayDetailVOS.isEmpty()){
            logger.warn(">>>>>>>>>>>>>优e贷还款明细查询数据为空<<<<<<<<<<<<<<<<");
            return;
        }
        try{
            String prdId = "";
            NlsProcessBizVO nlsProcessBizVO = new NlsProcessBizVO();
            for (LoanRepayDetailVO item : loanRepayDetailVOS){
                prdId = accLoanService.queryPrdId(item.getLoanNo());
                if ("11110004".equals(prdId)){
                    nlsProcessBizVO.setApplySeq(item.getSetlSeq());
                    NlsProcessBizVO queryNlsProcessBizVO = nlsProcessBizService.queryByPk(nlsProcessBizVO);
                    yedRepaymentInfoSyncService.doExecute(queryNlsProcessBizVO,item);
                }
            }
        } catch (Exception e){
            e.printStackTrace();
            logger.error(">>>>>>>>>>>>>优e贷还款计划任务执行失败<<<<<<<<<<<<<<<<");
        } finally {
            logger.info(">>>>>>>>>>>>>优e贷还款计划任务执行结束<<<<<<<<<<<<<<<<");
        }
    }
}
